package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.iw.adoms.entity.MsgNoticeType;
import com.iw.adoms.service.MsgNoticeTypeService;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/msgNoticeType")
public class MsgNoticeTypeAction {
	@Resource
	private MsgNoticeTypeService msgNoticeTypeService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<MsgNoticeType> pageData = new PageData<MsgNoticeType>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = msgNoticeTypeService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		return "/adoms/msgNoticeType/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		MsgNoticeType msgNoticeType = msgNoticeTypeService.find(id);
		model.addAttribute(msgNoticeType);
		return "/adoms/msgNoticeType/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(MsgNoticeType msgNoticeType) {
		if (msgNoticeType.isNew()) {
			msgNoticeTypeService.save(msgNoticeType);
		} else {
			msgNoticeTypeService.update(msgNoticeType);
		}

		return "redirect:/adoms/msgNoticeType/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		msgNoticeTypeService.delete(id);
		return "redirect:/adoms/msgNoticeType/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		msgNoticeTypeService.visible(id);
		return "redirect:/adoms/msgNoticeType/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		msgNoticeTypeService.unVisible(id);
		return "redirect:/adoms/msgNoticeType/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
